package cn.edu.scau.serviceImpl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.scau.dao.DownloadDao;
import cn.edu.scau.daoImpl.DownloadDaoImpl;
import cn.edu.scau.daoImpl.PeopleDaoImpl;
import cn.edu.scau.entity.Download;
import cn.edu.scau.entity.People;
import cn.edu.scau.entity.Work;
import cn.edu.scau.service.DownloadService;

/**
 * 下载信息的业务逻辑层
 * 
 * @author cxywan
 *
 */
@Service("downloadServiceImpl")
public class DownloadServiceImpl implements DownloadService<Download> {

	/**
	 * 持久化层
	 */
	@Autowired
	private DownloadDao downloadDao;

	public DownloadDao getDownloadDao() {
		return downloadDao;
	}

	public void setDownloadDao(DownloadDao downloadDao) {
		this.downloadDao = downloadDao;
	}

	/**
	 * 保存下载信息实体
	 * 
	 * @param entity
	 *            Download实体
	 */
	@Override
	public void add(Download entity) {

		this.getDownloadDao().save(entity);

	}

	/**
	 * 删除下载信息实体
	 * 
	 * @param entity
	 *            Download实体
	 */
	public boolean delete(Download entity, Integer id) {

		if (this.getDownloadDao().find(Download.class, id) != null) {
			this.getDownloadDao().delete(entity);
			return true;
		}

		return false;

	}

	/**
	 * 根据id删除下载信息实体
	 * 
	 * @param id
	 *            Download id
	 */
	@Override
	public boolean delete(Integer id) {

		Download download = this.getDownloadDao().find(Download.class, id);
		if (download != null) {

			this.getDownloadDao().delete(download);
			return true;
		}

		return false;

	}

	/**
	 * 更新下载信息实体
	 * 
	 * @param entity
	 *            Download实体
	 */
	@Override
	public boolean update(Download entity, Integer id) {

		if (this.getDownloadDao().find(Download.class, id) != null) {
			this.getDownloadDao().update(entity);
			return true;
		}

		return false;

	}

	/**
	 * 根据id获取下载信息实体
	 * 
	 * @param entity
	 *            Download id
	 */
	@Override
	public Download getDownloadByID(Integer id) {
		// TODO Auto-generated method stub
		return this.getDownloadDao().find(Download.class, id);
	}

	/**
	 * 获取页面总数
	 * 
	 * @param columnID
	 *            栏目ID
	 * @param pageSize
	 *            每一页要展示的条目总数
	 * @param frontOrBack
	 *            前台还是后台获取
	 */
	public long countTheEntityNum(Integer columnID, Integer pageSize, boolean frontOrBack) {

		Long count = (long) 0;

		// 分前后台获取条目总数
		if (frontOrBack) {

			count = this.getDownloadDao().getDownloadsCountFront();
			System.out.println(count);

		} else {

			count = this.getDownloadDao().findCount(Download.class);
			System.out.println(count);
		}

		return count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
	}

	/**
	 * 获取前后或者后台的download属性
	 * 
	 * @param columnID
	 *            栏目ID
	 * @param pageNO
	 *            第几页
	 * @param pageSize
	 *            每一页要展示的条目总数
	 * @param frontOrBack
	 *            前台还是后台获取
	 */
	public List getDownloadsProperty(Integer columnID, Integer pageNo, Integer pageSize, Boolean frontOrBack) {

		if (frontOrBack) {
			return this.getDownloadDao().getPropertyOfDownloadFront(pageNo, pageSize);
		} else {
			return this.getDownloadDao().getPropertyOfDownloadManagement(pageNo, pageSize);
		}
	}

	/**
	 * 下载排行榜
	 * 
	 * @param size
	 *            要展示的条数
	 */
	@Override
	public List getDownloadsByRank(int size) {

		return this.getDownloadDao().getDownloadsByLikeTimes(size);
	}

	@Override
	public List showTheEntity(String type, Integer columnId, Integer id, Integer pageNo, boolean frontOrBackground) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> saveImage(MultipartFile[] files, HttpServletRequest request, String releaseDate) {
		// TODO Auto-generated method stub
		return null;
	}

}
